package cn.e4j.modular.api.controller;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.modular.api.service.BfcglService;
import cn.e4j.modular.api.service.ShhbService;
import cn.e4j.modular.api.upload.ImgUtils;
import cn.e4j.modular.api.util.CommonUtils;
import cn.e4j.modular.api.util.ControllerSupport;
import cn.e4j.modular.crm.entity.GwecT;
import cn.e4j.modular.system.entity.SysUser;
import cn.e4j.modular.system.service.impl.SystemServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 送货回报
 */
@RestController
@RequestMapping("api/wx/shhb")
public class ShhbController extends ControllerSupport {
    @Autowired
    SystemServiceImpl systemService;

    @Autowired
    ShhbService shhbService;

    @RequestMapping(value = "getList")
    public ResponseData getList(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();
        List<Dto> result = new ArrayList<>();

        try {
            String type = inDto.getString("type");
            //上下级权限
            Dto sys003 = systemService.getSys003(currentUser);
            inDto.put("ORGS", sys003.get("ORGS"));

            inDto.put("type",type==null?1:type);
            result = shhbService.getList(wapper.getPage(),inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS(result);
    }


    @RequestMapping(value = "save")
    public ResponseData save(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> result = new ArrayList<>();
        try {
        GwecT gwecT = CommonUtils.toJavaClass(inDto, GwecT.class);
        if (inDto.getString("GWEC103").equals("Y")){
            //说明进行回报操作
            ResponseData data = ImgUtils.base64ToImg(inDto.getString("signature"));
            if (data.getCode() == 200){
                gwecT.setGWEC024(data.getData()+"");
            }
        }
            gwecT.updateById();
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS(result);
    }
}
